package com.itheima.controller.backend;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "预约设置管理")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;
    @GetMapping("/download")
    @ApiOperation("模版下载")
    public Result downloadModel(HttpServletResponse response) {
        log.info("模版下载");
        try {
            orderSettingService.downloadModel(response);
            // 模版下载
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "模版下载失败");
        }
        return new Result(true, "模版下载成功");
    }

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByOrderDate")
    @ApiOperation("根据日期修改可预约人数")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumberByOrderDate(orderSetting);
        return new Result(true,"预约设置成功");
    }

    /**
     * 根据日期获取预约设置数据
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    @ApiOperation("根据日期获取预约设置数据")
    public Result getOrderSettingByMonth(String month){
        log.info("根据日期获取预约设置数据:{}",month);
        List<OrderSetting> orderSettingList = orderSettingService.getOrderSettingByMonth(month);
        return new Result(true,"获取预约设置数据成功",orderSettingList);
    }

    /**
     * 批量导入预约设置数据
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("批量导入预约设置数据")
    public Result upload(@RequestParam MultipartFile  excelFile){
        log.info("批量导入预约设置数据");
        try {

            orderSettingService.upload(excelFile);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"批量导入预约设置数据失败");
        }
        return new Result(true,"批量导入预约设置数据成功");
    }
}
